﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HandBrake_Encoder
{
	public partial class FormScheduler : Form
	{
		private DateTime scheduledRunTime;


		public DateTime timeToStart
		{
			get { return scheduledRunTime; }
			set { scheduledRunTime = value; }
		}

		public FormScheduler()
		{
			InitializeComponent();
		}

		private void FormScheduler_Load(object sender, EventArgs e)
		{
			dateTimePicker1.Format = DateTimePickerFormat.Time;
			dateTimePicker1.Value = DateTime.Now;

			SetupTimerControlLocs();
		}


		// Move the timer controls at same loc as datetimepicker control
		private void SetupTimerControlLocs()
		{
			// Move Timer Controls
			numericUpDownHours.Top = dateTimePicker1.Top;
			numericUpDownMinutes.Top = dateTimePicker1.Top;
			numericUpDownSeconds.Top = dateTimePicker1.Top;
			labelTimeColon1.Top = dateTimePicker1.Top;
			labelTimeColon2.Top = dateTimePicker1.Top;
		}



		// Input option was changed from StartTime to duration timer
		private void radioButTimer_CheckedChanged(object sender, EventArgs e)
		{
			dateTimePicker1.Visible = false;
			numericUpDownHours.Visible = true;
			numericUpDownMinutes.Visible = true;
			numericUpDownSeconds.Visible = true;
			labelTimeColon1.Visible = true;
			labelTimeColon2.Visible = true;
		}

		// Input option was changed from duration timer to Start time
		private void radioButStartTime_CheckedChanged(object sender, EventArgs e)
		{
			dateTimePicker1.Visible = true;
			numericUpDownHours.Visible = false;
			numericUpDownMinutes.Visible = false;
			numericUpDownSeconds.Visible = false;
			labelTimeColon1.Visible = false;
			labelTimeColon2.Visible = false;
		}


		// Notifys the user if a very short delay was entered.  Prevents starting a batch encode
		//		if a short time was entered by mistake.
		private bool CheckForShortTimeOK()
		{
			if (scheduledRunTime.Subtract(DateTime.Now).TotalMinutes < 1)
			{
				DialogResult = MessageBox.Show("The Start Time is less than 1 minute from now.\r\n" +
												"\r\nSchedule anyway?", "Short Delay",
												MessageBoxButtons.YesNo, MessageBoxIcon.Information);

				if (DialogResult == DialogResult.Yes) return true;
				
				return false;
			}
			return true;
			
		}


		// OK button was pressed
		private void buttonOK_Click(object sender, EventArgs e)
		{
			// If input was timer then update the run time
			if (radioButTimer.Checked)
			{
				scheduledRunTime = DateTime.Now;
				scheduledRunTime = scheduledRunTime.AddHours((double)numericUpDownHours.Value);
				scheduledRunTime = scheduledRunTime.AddMinutes((double)numericUpDownMinutes.Value);
				scheduledRunTime = scheduledRunTime.AddSeconds((double)numericUpDownSeconds.Value);
			}

			if (CheckForShortTimeOK()) DialogResult = DialogResult.OK;
			else DialogResult = DialogResult.None;
		}







		// Cancel button was pressed
		private void buttonCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
		}


		// Change the Start time of the scheduled batch encode to the value of the datetimepicker value
		//		Compensate for next day, such as A.M. input when current time is P.M.
		private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
		{
			if (dateTimePicker1.Value < DateTime.Now) scheduledRunTime = dateTimePicker1.Value.AddDays(1);
			else { scheduledRunTime = dateTimePicker1.Value; }
		}

		// Change the start time by the number of hours
		private void numericUpDownHours_ValueChanged(object sender, EventArgs e)
		{

		}
	}
}
